// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) // 使用当前云环境
const { notifyOrderToStaff } = require('./service/messageNotifyService');
const { checkout, paoTuiPriceCompute, paoTuiCheckOut, xiXiePriceCompute,xiXieCheckoutInit,
  kuaiDiDaiQuCheckoutInit, printCheckoutInit, printComputePrice, kuaiDiDaiQuComputePrice
 } = require('./service/checkoutService');
const { createOrder, paoTuiCreateOrder,xiXieCreateOrder, kuaiDiCreateOrder,  printCreateOrder, snacksCreateOrder, cancelOrder, updateOrderStatus, processRefund, handleRejectRefund} = require('./service/createOrderService');
const { paySuccessCallback, paoTuiPaySuccessCallback} = require('./service/paySuccessCallbackService');
const { printTicket } = require('./service/PrintService');
const { getHomeInfo, getProductList } = require('./service/getHomeInfoService');
const rpcUtils = require('./utils/rpcUtils');
const reconOrderTask = require('./tasks/reconOrderTask');
const { createBargain, helpBargain,getBargainDetail} = require('./BargainService');
const updateSchoolLocationTask = require('./tasks/updateSchoolLocationTask');
const { getOrderList, getOrderListForManage, getOrderForDetail, getOrderStatistics } = require('./service/getOrderListService');
const { addAddress, getAvaliableSchoolList } = require('./service/AddressMgtService');
const { claimCoupon, getCouponsByUserId, showCouponsBySchool } = require('./service/CouponService');
const { getConfigByNameSchoolProduct } = require('./service/ConfigService');
const { getUserInfo, updateUserInfo } = require('./service/UserService');
const { getComplaintList, saveComplaint, updateComplaintStatus } = require('./service/ComplaintService');
const { getSubProductPageInfo, getSubProductListForBuilding, getBuildingListBySchoolAdmin, getSnacksListForAdmin, updateSnackStatusAdmin, saveSnackAdmin, getBuildingDeliveryFee, updateBuildingDeliveryFee, getSnacksOrdersForAdmin, updateSnacksOrderStatus } = require('./service/SnacksStoreService');

// 云函数入口函数
exports.main = async (event, context) => {
  try {
    const wxContext = cloud.getWXContext()
    console.log("xiaoBangCommomFunction.params",JSON.stringify(event))
    let {
      notifyOrderRequest,
      checkoutRequest,
      createOrderRequest,
      cancelOrderRequest,
      addAddressRequest,
      getAvaliableSchoolListRequest,
      claimCouponRequest,
      paySuccessCallbackRequest,
      printTicketRequest,
      getHomeInfoRequest,
      getOrderListRequest,
      TriggerName,
      createBargainRequest,
      helpBargainRequest,
      getBargainDetailRequest,
      getOrderListForManageRequest,
      getOrderForDetailRequest,
      paoTuiPriceComputeRequest,
      paoTuiCheckOutRequest,
      paoTuiCreateOrderRequest,
      paoTuiPaySuccessCallbackRequest,
      xiXieCheckoutInitRequest,
      xiXieComputePriceRequest,
      xiXieCreateOrderRequest,
      kuaiDiCheckoutInitRequest,
      kuaiDiCreateOrderRequest,
      kuaiDiDaiQuComputePriceRequest,
      printCheckoutInitRequest,
      printComputePriceRequest,
      printCreateOrderRequest,
      getConfigRequest,
      getCouponsByUserIdRequest,
      getUserInfoRequest,
      getComplaintListRequest,
      saveComplaintRequest,
      showCouponsBySchoolRequest,
      updateComplaintStatusRequest,
      updateOrderStatusRequest,
      getProductListRequest,
      processRefundRequest,
      handleRejectRefundRequest,
      getOrderStatisticsRequest,
      updateUserInfoRequest,
      getSubProductPageInfoRequest,
      getSubProductListForBuildingRequest,
      getBuildingListBySchoolAdminRequest,
      getSnacksListForAdminRequest,
      updateSnackStatusAdminRequest,
      saveSnackAdminRequest,
      getBuildingDeliveryFeeRequest,
      updateBuildingDeliveryFeeRequest,
      snacksCreateOrderRequest,
      getSnacksOrdersForAdminRequest,
      updateSnacksOrderStatusRequest,
    } = event;

    let result = null;

    if (notifyOrderRequest) {
      return await notifyOrderToStaff(notifyOrderRequest)
    }
    else if(checkoutRequest){
      return await checkout(checkoutRequest)
    }
    else if(createOrderRequest){
      return await createOrder(createOrderRequest)
    }
    else if(cancelOrderRequest){
      return await cancelOrder(cancelOrderRequest)
    }
    else if(addAddressRequest){
      return await addAddress(addAddressRequest)
    }
    else if(getAvaliableSchoolListRequest){
      return await getAvaliableSchoolList(getAvaliableSchoolListRequest)
    }
    else if(claimCouponRequest){
      return await claimCoupon(claimCouponRequest)
    }
    else if(paySuccessCallbackRequest){
      return await paySuccessCallback(paySuccessCallbackRequest)
    }
    else if(printTicketRequest){
      return await printTicket(printTicketRequest)
    }
    else if(getHomeInfoRequest){
      return await getHomeInfo(getHomeInfoRequest)
    }
    else if (TriggerName === "reconOrderTask") {
      return await reconOrderTask(event)
    }
    else if (TriggerName === "warmUpTrigger") {
      return {
        code: 200,
        message: 'warmUpTrigger',
        data: null
      };
    }
    else if (createBargainRequest) {
      createBargainRequest.openId = createBargainRequest.userId || wxContext.OPENID
      result = await createBargain(createBargainRequest)
    }
    else if (helpBargainRequest) {
      helpBargainRequest.openId = helpBargainRequest.helperId || wxContext.OPENID
      result = await helpBargain(helpBargainRequest)
    }
    else if (getBargainDetailRequest) {
      result = await getBargainDetail(getBargainDetailRequest)
    }
    else if (TriggerName === "updateSchoolLocationTask") {
      return await updateSchoolLocationTask(event)
    }
    else if (getOrderListRequest) {
      return await getOrderList(getOrderListRequest);
    }
    else if (getOrderListForManageRequest) {
      getOrderListForManageRequest.userId = getOrderListForManageRequest.userId || wxContext.OPENID
      return await getOrderListForManage(getOrderListForManageRequest);
    }
    else if (getOrderForDetailRequest) {
      return await getOrderForDetail(getOrderForDetailRequest);
    }
    else if (paoTuiPriceComputeRequest) {
      paoTuiPriceComputeRequest.openId = paoTuiPriceComputeRequest.openId || wxContext.OPENID
      result = await paoTuiPriceCompute(paoTuiPriceComputeRequest)
    }
    else if (paoTuiCheckOutRequest) {
      paoTuiCheckOutRequest.openId = paoTuiCheckOutRequest.openId || wxContext.OPENID
      result = await paoTuiCheckOut(paoTuiCheckOutRequest)
    }
    else if (paoTuiCreateOrderRequest) {
      paoTuiCreateOrderRequest.openId = paoTuiCreateOrderRequest.openId || wxContext.OPENID
      result = await paoTuiCreateOrder(paoTuiCreateOrderRequest)
    }
    else if (paoTuiPaySuccessCallbackRequest) {
      paoTuiPaySuccessCallbackRequest.openId = paoTuiPaySuccessCallbackRequest.openId || wxContext.OPENID
      result = await paoTuiPaySuccessCallback(paoTuiPaySuccessCallbackRequest)
    }
    else if(xiXieComputePriceRequest) {
      return await xiXiePriceCompute(xiXieComputePriceRequest)
    }
    else if (xiXieCheckoutInitRequest) {
      xiXieCheckoutInitRequest.openId = xiXieCheckoutInitRequest.openId || wxContext.OPENID;
      return await xiXieCheckoutInit(xiXieCheckoutInitRequest)
    }
    else if (xiXieCreateOrderRequest) {
      xiXieCreateOrderRequest.openId = xiXieCreateOrderRequest.openId || wxContext.OPENID;
      return await xiXieCreateOrder(xiXieCreateOrderRequest)
    }
    else if (kuaiDiCheckoutInitRequest) {
      kuaiDiCheckoutInitRequest.openId = kuaiDiCheckoutInitRequest.openId || wxContext.OPENID;
      return await kuaiDiDaiQuCheckoutInit(kuaiDiCheckoutInitRequest)
    }
    else if (kuaiDiCreateOrderRequest) {
      kuaiDiCreateOrderRequest.openId = kuaiDiCreateOrderRequest.openId || wxContext.OPENID;
      return await kuaiDiCreateOrder(kuaiDiCreateOrderRequest)
    }
    else if (printCheckoutInitRequest) {
      printCheckoutInitRequest.openId = printCheckoutInitRequest.openId || wxContext.OPENID;
      return await printCheckoutInit(printCheckoutInitRequest)
    }
    else if (printComputePriceRequest) {
      printComputePriceRequest.openId = printComputePriceRequest.openId || wxContext.OPENID;
      return await printComputePrice(printComputePriceRequest)
    }
    else if (printCreateOrderRequest) {
      printCreateOrderRequest.openId = printCreateOrderRequest.openId || wxContext.OPENID;
      return await printCreateOrder(printCreateOrderRequest)
    }
    else if (getConfigRequest) {
      return await getConfigByNameSchoolProduct(getConfigRequest)
    }
    else if (getCouponsByUserIdRequest) {
      return await getCouponsByUserId(getCouponsByUserIdRequest)
    }
    else if (getUserInfoRequest) {
      return await getUserInfo(getUserInfoRequest)
    }
    else if (getComplaintListRequest) {
      return await getComplaintList(getComplaintListRequest)
    }
    else if (saveComplaintRequest) {
      return await saveComplaint(saveComplaintRequest)
    }
    else if (showCouponsBySchoolRequest) {
      return await showCouponsBySchool(showCouponsBySchoolRequest)
    }
    else if (updateComplaintStatusRequest) {
      return await updateComplaintStatus(updateComplaintStatusRequest)
    }
    else if (updateOrderStatusRequest) {
      return await updateOrderStatus(updateOrderStatusRequest)
    }
    else if (kuaiDiDaiQuComputePriceRequest) {
      result  = await kuaiDiDaiQuComputePrice(kuaiDiDaiQuComputePriceRequest)
    }
    else if (getProductListRequest) {
      return await getProductList(getProductListRequest)
    }
    else if (processRefundRequest) {
      return await processRefund(processRefundRequest)
    }
    else if (handleRejectRefundRequest) {
      return await handleRejectRefund(handleRejectRefundRequest)
    }
    else if (getOrderStatisticsRequest) {
      return await getOrderStatistics(getOrderStatisticsRequest)
    }
    else if (updateUserInfoRequest) {
      return await updateUserInfo(updateUserInfoRequest)
    }
    else if (getSubProductPageInfoRequest) {
      return await getSubProductPageInfo(getSubProductPageInfoRequest)
    }
    else if (getSubProductListForBuildingRequest) {
      return await getSubProductListForBuilding(getSubProductListForBuildingRequest)
    }
    else if (getBuildingListBySchoolAdminRequest) {
      return await getBuildingListBySchoolAdmin(getBuildingListBySchoolAdminRequest)
    }
    else if (getSnacksListForAdminRequest) {
      return await getSnacksListForAdmin(getSnacksListForAdminRequest)
    }
    else if (updateSnackStatusAdminRequest) {
      return await updateSnackStatusAdmin(updateSnackStatusAdminRequest)
    }
    else if (saveSnackAdminRequest) {
      return await saveSnackAdmin(saveSnackAdminRequest)
    }
    else if (getBuildingDeliveryFeeRequest) {
      return await getBuildingDeliveryFee(getBuildingDeliveryFeeRequest)
    }
    else if (updateBuildingDeliveryFeeRequest) {
      return await updateBuildingDeliveryFee(updateBuildingDeliveryFeeRequest)
    }
    else if (snacksCreateOrderRequest) {
      return await snacksCreateOrder(snacksCreateOrderRequest)
    }
    else if (getSnacksOrdersForAdminRequest) {
      return await getSnacksOrdersForAdmin(getSnacksOrdersForAdminRequest)
    }
    else if (updateSnacksOrderStatusRequest) {
      return await updateSnacksOrderStatus(updateSnacksOrderStatusRequest)
    }
    else {
      throw new Error('request params is required');
    }

    return result;

  } catch (error) {
    console.error("Error:", error.stack || error);
    return rpcUtils.genResp(rpcUtils.codeEnum.ERROR, "Error processing request", null);
  }
}